package com.gmail.hh.flickrj.sample.android.fragments;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.gmail.hh.flickrj.sample.android.FlickrjMainActivity;
import com.gmail.hh.flickrj.sample.android.R;
import com.gmail.hh.flickrj.sample.android.data.DataManagerFlickr;
import com.gmail.hh.flickrj.sample.android.data.PhotoData;
import com.gmail.hh.flickrj.sample.android.data.PhotoData.IPhotoPositionShift;
import com.gmail.hh.flickrj.sample.android.data.PhotolistData;
import com.gmail.hh.flickrj.sample.android.helper.SettingsManager;
import com.gmail.hh.flickrj.sample.android.listener.ImageViewZoomListener;


@SuppressLint("NewApi")
public class FragmentPhoto extends Fragment 
	implements IPhotoPositionShift{

	private static final Logger logger = LoggerFactory.getLogger(FlickrjMainActivity.class);

	public static final String TAG = "fragment_photo";
	private PhotoData photoData;
	private PhotolistData photolistData;
	private int position;
	private ImageView imageViewLarge;
	private ImageViewZoomListener mImageLargeListener;
	private ImageButton centerButton;
	
	@Override
	public void onCreate(Bundle arg0) {

		super.onCreate(arg0);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
		// Inflate the layout file.
		View view = inflater.inflate(R.layout.fragment_photo, null);

		this.imageViewLarge = (ImageView) view.findViewById(R.id.imageLarge);
		this.centerButton = (ImageButton) view.findViewById(R.id.btnCenter);
		
		// set Listener to the image for zooming
		// imageLarge.setOnClickListener( mImageLargeListener );
		// imageLarge.setOnLongClickListener( mImageLargeListener );
		mImageLargeListener = new ImageViewZoomListener();
		imageViewLarge.setOnTouchListener( mImageLargeListener );
		this.centerButton.setOnClickListener(mImageLargeListener);
		this.centerButton.setOnLongClickListener(mImageLargeListener);

		return view;
	}

	@Override
	public void onViewCreated(View arg0, Bundle arg1) {
		super.onViewCreated(arg0, arg1);

	}
	

	public void setPhotoData(PhotoData photoData, PhotolistData photolistData, int position, long delayMSEC) {

		// try to use existing data from PhotoData
		photoData.resetMatrix(); 
		Drawable drawable = null;
		String imageCenterRot = SettingsManager.getInstance().getImageCenterRot();
		if (photoData.getImageViewMedium()!=null) {
			drawable = photoData.getImageViewMediumDrawable();
			if (drawable!=null) {
				photoData.setImageViewMedium(imageViewLarge);
				imageViewLarge.setImageDrawable(drawable);
				photoData.centerMatrix(imageCenterRot, imageViewLarge);
				logger.error("use existing Medium: "  );
			}
		}
		if (drawable==null && photoData.getImageViewSmallSquare()!=null) {
			drawable = photoData.getImageViewSmallSquare().getDrawable();
			if (drawable!=null) {
				photoData.setImageViewMedium(imageViewLarge);
				imageViewLarge.setImageDrawable(drawable);
				photoData.centerMatrix(imageCenterRot, imageViewLarge);
				logger.error("use existing Small: "  );
			}
		}

		logger.error("set PhotoData: " + photoData.toString() );

		// set the imageView of this fragment into the photoData
		this.photoData = photoData;
		this.photolistData = photolistData;
		this.position = position;
		photoData.setImageViewMedium(imageViewLarge);
		
		//load Photo-Bitmap
		boolean showProgress = true;
		DataManagerFlickr.getInstance().loadPhotoImageMedium(photoData, showProgress, delayMSEC);
		
		// actualize the photoData object in the Listener
		mImageLargeListener.setPhotoData(photoData);
		mImageLargeListener.setIPhotoPositionShift(this);
		
	}

	@Override
	public void onPhotoPositionShift(String moveDirection) {
		if (this.photolistData!=null ) {
			int nextPosition = this.photolistData.moveInPhotolist(moveDirection, this.position);
	//		if (nextPosition!=this.position) {
				logger.error("Double-touch for moveDirection: " + moveDirection);
				logger.error("actPos: " + this.position + "   nextPos: " + nextPosition);
				setPhotoData(this.photolistData.getAtPosition(nextPosition), this.photolistData, nextPosition, 0l);
	//		}
		}
			
		
	}
	
	


}
